package io.seata.samples.sc.tccat.consumer.controller;

import io.seata.samples.sc.tccat.consumer.service.TccTransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 发起事务控制器
 *
 * @author lenovo
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("tcc")
public class TccController {
    private final TccTransactionService tccTransactionService;

    /**
     * 分布式事务提交示例接口
     */
    @GetMapping("/testCommit")
    public String testCommit() {
        String result = tccTransactionService.testCommit();
        return "----------test transaction commit---------- \n" + result;
    }

    /**
     * 分布式事务回滚示例接口
     */
    @GetMapping("/testRollback")
    public String testRollback() {
        String result = "";
        try {
            tccTransactionService.testRollback();
        } catch (Throwable t) {
            result = t.getMessage();
        }
        return "----------test transaction rollback ---------- \n" + result;
    }
}
